import 'dart:io';
import 'dart:math';

import 'package:flutter/material.dart';

import '../entities/entities.dart';
import '../entities/entities_data.dart';

class BannersScreen extends StatelessWidget {
  const BannersScreen({super.key});

  @override
  Widget build(BuildContext context) {
    var columnCountSetting = 3;
    var rat = 1080.0 / 1920.0 / 3;
    var size = MediaQuery.of(context).size;
    var sw = min(size.width, size.height);
    var sh = max(size.width, size.height);
    int columnInt = 2;
    if (columnCountSetting != 0) {
      columnInt = columnCountSetting;
    } else {
      var column = sw / sh / rat;
      var columnInt = column.floor();
      if (columnInt == 0) {
        columnInt = 1;
      }
    }
    var w = sw / (columnInt + 0.3);

    return ListView.builder(
        itemBuilder: (context, index) {
          return _buildBanner(banners[index], w);
        },
        itemCount: banners.length);
  }

  Widget _buildBanner(BannerInfo banner, double w) {
    var column = Column(
      children: [
        const SizedBox(height: 15),
        Text(
          "v${banner.V}",
          style: const TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 15),
        ...banner.P.map((e) => _buildBannerP(e, w)),
      ],
    );
    return Container(
      decoration: BoxDecoration(
        color: Colors.grey.shade300.withAlpha(80),
        borderRadius: BorderRadius.circular(10),
      ),
      margin: const EdgeInsets.all(10),
      child: column,
    );
  }

  Widget _buildBannerP(BannerP bannerP, double w) {
    return Column(
      children: [
        Text(
          _idToName(bannerP.id),
          style: const TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 10),
        Wrap(
          children: bannerP.A
              .map((e) => _buildBannerAB(e, w, const Color(0xffffa95d)))
              .toList(),
        ),
        Wrap(
          children: bannerP.B
              .map((e) => _buildBannerAB(e, w, const Color(0xff4a88e3)))
              .toList(),
        ),
        const SizedBox(height: 15),
      ],
    );
  }

  String _idToName(int id) {
    if (id == 0) {
      return "?";
    }
    if (id == 4) {
      return "混池";
    }
    return "第$id期";
  }

  Widget _buildBannerAB(BannerAB bannerAB, double w, Color color) {
    return SizedBox(
      width: w,
      child: Card(
        color: color,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10),
        ),
        margin: const EdgeInsets.all(10),
        child: Stack(
          children: [
            Container(
              padding: EdgeInsets.all(w / 15),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                gradient: const LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  tileMode: TileMode.repeated,
                  colors: [
                    Color(0x55000000),
                    Color(0x44000000),
                    Color(0x33000000),
                    Color(0x44000000),
                    Color(0x55000000),
                  ],
                ),
              ),
              child: LayoutBuilder(
                builder: (BuildContext context, BoxConstraints constraints) {
                  return Image.file(
                    File(
                        '$repo/pics/Character/UI_AvatarIcon_${bannerAB.N}.png'),
                    width: constraints.maxWidth,
                    height: constraints.maxWidth,
                  );
                },
              ),
            ),
            Align(
              alignment: Alignment.topRight,
              child: Container(
                margin: EdgeInsets.only(
                  top: w / 40,
                  right: w / 40,
                ),
                decoration: BoxDecoration(
                  color: Colors.grey.shade300.withAlpha(20),
                  borderRadius: BorderRadius.circular(100),
                ),
                width: 28,
                height: 28,
                alignment: Alignment.center,
                child: Text(
                  bannerAB.X.toString(),
                  style: const TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.bold,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
